package com.example.cemenghuii.service;

import com.example.cemenghuii.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Service
public class FileService {

    @Autowired
    private FileUtil fileUtil;

    @Value("${file.upload.max-size:104857600}") // 默认100MB
    private long maxFileSize;

    // 支持的图片类型
    private static final List<String> ALLOWED_IMAGE_TYPES = Arrays.asList(
            "image/jpeg", "image/jpg", "image/png", "image/gif", "image/bmp", "image/webp"
    );

    // 支持的视频类型
    private static final List<String> ALLOWED_VIDEO_TYPES = Arrays.asList(
            "video/mp4", "video/avi", "video/mov", "video/wmv", "video/flv", "video/webm"
    );

    // 支持的文档类型
    private static final List<String> ALLOWED_DOCUMENT_TYPES = Arrays.asList(
            "application/pdf", "application/msword",
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
            "application/vnd.ms-excel",
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            "application/vnd.ms-powerpoint",
            "application/vnd.openxmlformats-officedocument.presentationml.presentation"
    );

    /**
     * 上传图片文件
     */
    public String uploadImage(MultipartFile file) throws IOException {
        validateFile(file, ALLOWED_IMAGE_TYPES, "图片");
        return fileUtil.uploadFile(file, "images");
    }

    /**
     * 上传视频文件
     */
    public String uploadVideo(MultipartFile file) throws IOException {
        validateFile(file, ALLOWED_VIDEO_TYPES, "视频");
        return fileUtil.uploadFile(file, "videos");
    }

    /**
     * 上传文档文件
     */
    public String uploadDocument(MultipartFile file) throws IOException {
        validateFile(file, ALLOWED_DOCUMENT_TYPES, "文档");
        return fileUtil.uploadFile(file, "documents");
    }

    /**
     * 上传通用文件
     */
    public String uploadFile(MultipartFile file, String folder) throws IOException {
        if (file.isEmpty()) {
            throw new IOException("文件不能为空");
        }

        // 检查文件大小
        if (!fileUtil.checkFileSize(file, maxFileSize)) {
            throw new IOException("文件大小超过限制：" + (maxFileSize / 1024 / 1024) + "MB");
        }

        return fileUtil.uploadFile(file, folder);
    }

    /**
     * 删除文件
     */
    public boolean deleteFile(String filePath) {
        if (filePath == null || filePath.isEmpty()) {
            return false;
        }
        return fileUtil.deleteFile(filePath);
    }

    /**
     * 检查文件是否存在
     */
    public boolean fileExists(String filePath) {
        return fileUtil.fileExists(filePath);
    }

    /**
     * 获取文件完整路径
     */
    public String getFullPath(String relativePath) {
        return fileUtil.getFullPath(relativePath);
    }

    /**
     * 验证文件
     */
    private void validateFile(MultipartFile file, List<String> allowedTypes, String fileTypeName) throws IOException {
        if (file.isEmpty()) {
            throw new IOException(fileTypeName + "文件不能为空");
        }

        // 检查文件大小
        if (!fileUtil.checkFileSize(file, maxFileSize)) {
            throw new IOException("文件大小超过限制：" + (maxFileSize / 1024 / 1024) + "MB");
        }

        // 检查文件类型
        String contentType = file.getContentType();
        if (contentType == null || !allowedTypes.contains(contentType.toLowerCase())) {
            throw new IOException("不支持的" + fileTypeName + "格式：" + contentType);
        }
    }

    /**
     * 根据文件类型上传文件
     */
    public String uploadFileByType(MultipartFile file) throws IOException {
        String contentType = file.getContentType();

        if (contentType != null) {
            if (contentType.startsWith("image/")) {
                return uploadImage(file);
            } else if (contentType.startsWith("video/")) {
                return uploadVideo(file);
            } else if (ALLOWED_DOCUMENT_TYPES.contains(contentType)) {
                return uploadDocument(file);
            }
        }

        // 默认上传到通用文件夹
        return uploadFile(file, "files");
    }

    /**
     * 批量删除文件
     */
    public boolean deleteFiles(List<String> filePaths) {
        boolean allSuccess = true;
        for (String filePath : filePaths) {
            if (!deleteFile(filePath)) {
                allSuccess = false;
            }
        }
        return allSuccess;
    }

    /**
     * 获取文件类型
     */
    public String getFileType(String contentType) {
        if (contentType == null) {
            return "unknown";
        }

        if (contentType.startsWith("image/")) {
            return "image";
        } else if (contentType.startsWith("video/")) {
            return "video";
        } else if (ALLOWED_DOCUMENT_TYPES.contains(contentType)) {
            return "document";
        } else {
            return "file";
        }
    }
}